//
//  determineView.m
//  zhangGuangTianXia
//
//  Created by tangtangchitangtang on 2017/11/9.
//  Copyright © 2017年 tangtangchitangtang. All rights reserved.
//

#import "determineView.h"

@implementation determineView

-(void)creatView{
    UIView *textView2=[UIView new];
    [self addSubview:textView2];
    [textView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(130);
    }];
    textView2.backgroundColor=ColorWithRGB(240, 240, 240);
    textView2.layer.cornerRadius=25;
    
    
    _codeLabel=[UILabel new];
    [textView2 addSubview:_codeLabel];
    [_codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(0);
    }];
    _codeLabel.textColor=_tianLanColor;
    _codeLabel.font=[UIFont systemFontOfSize:14];
    _codeLabel.text=@"获取验证码";
    [_codeLabel layoutIfNeeded];
    _codeLabel.userInteractionEnabled=YES;
    UITapGestureRecognizer *huoQuYan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(huoQuYanZhen:)];
    [_codeLabel addGestureRecognizer:huoQuYan];
    
    _codeText=[UITextField new];
    [textView2 addSubview:_codeText];
    [_codeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(0);
    }];
    _codeText.placeholder=@"短信验证码";
    _codeText.textColor=ColorWithRGB(49, 49, 49);
    _codeText.font=[UIFont systemFontOfSize:17];
    _codeText.keyboardType=UIKeyboardTypePhonePad;
    _codeText.returnKeyType=UIReturnKeyDone;

    
    UIButton *btn=[UIButton new];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(textView2);
        make.top.mas_equalTo(textView2.mas_bottom).mas_equalTo(40);
        make.height.mas_equalTo(50);
    }];
    btn.backgroundColor=_tianLanColor;
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    [btn setTitle:@"统一协议并注册" forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:17];
    btn.layer.cornerRadius=25;
    [btn addTarget:self action:@selector(registerBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    NSString *xieYiStr=@"注册表示您已同意《掌通天下用户协议》";

    UILabel *xieYiLabel=[UILabel new];
    [self addSubview:xieYiLabel];
    [xieYiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(btn.mas_bottom).mas_equalTo(10);
    }];

    NSMutableAttributedString *AttributedStr= [[NSMutableAttributedString alloc]initWithString:xieYiStr];

    [AttributedStr addAttribute:NSForegroundColorAttributeName value:ColorWithRGB(200, 200, 200) range:NSMakeRange(0, 8)];
    [AttributedStr addAttribute:NSForegroundColorAttributeName value:_tianLanColor range:NSMakeRange(8, 10)];
    xieYiLabel.font=[UIFont systemFontOfSize:13];
    xieYiLabel.attributedText=AttributedStr;
}

-(IBAction)registerBtn:(id)sender{
    NSString *codeStr=_codeText.text;
    if ([ZQTools charIsNil:codeStr]) {
        [ZQTools svpInfo:@"验证码不能为空"];
        return;
    }
    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:_phoneStr,@"phone",_passwordStr,@"password",_userNameStr,@"nickName",codeStr,@"smsCode",@"1",@"operatesys", nil];
    
    [ZQTools AFNPostDataUrl:@"suser/userRegister" Dict:dic andTableView:nil andView:self andSuccessBlock:^(id responseObject) {
        BaseModel *dataModel = [BaseModel mj_objectWithKeyValues:responseObject];
        if ([dataModel.status isEqualToString:@"200"]) {
            
            [NSKeyedArchiver archiveRootObject:dataModel.data toFile:_userModelFile];
            
        }
        else{
            [ZQTools svpInfo:dataModel.msg];
        }

    } anderrorBlock:nil];

    
}

-(void)huoQuYanZhen:(UITapGestureRecognizer *)tap{
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:_phoneStr,@"phone",@"REGISTER",@"type",nil];
    
    [ZQTools AFNPostDataUrl:@"msmscode/setSmscode" Dict:dict andTableView:nil andView:nil andSuccessBlock:^(id responseObject) {
        BaseModel *dataModel = [BaseModel mj_objectWithKeyValues:responseObject];
        if ([dataModel.status isEqualToString:@"200"]) {
            _codeLabel.userInteractionEnabled=NO;
            _timeTick=60;
            _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
        }
        else{
            [ZQTools svpInfo:dataModel.msg];
        }
        
    } anderrorBlock:nil];
    
    
  
}
-(void)timeFireMethod
{
    _codeLabel.userInteractionEnabled=NO;
    if (_timeTick==0) {
        _codeLabel.userInteractionEnabled=YES;
        _codeLabel.text=@"获取验证码";
        [_timer invalidate];
        return;
    }
    _timeTick--;
    _codeLabel.text=[NSString stringWithFormat:@"重新发送(%dS)",_timeTick];
}





@end
